package com.young.common.core.dal.service.impl;

import java.util.List;

import com.young.common.core.dal.DalRowBounds;
import com.young.common.core.dal.EDBType;

import com.young.common.util.Page;
import com.young.common.core.dal.service.IDataAccessService;
import org.mybatis.spring.SqlSessionTemplate;

import javax.annotation.Resource;

public class DataAccessServiceImpl implements IDataAccessService {

	@Resource
	private SqlSessionTemplate sqlSessionTemplate;

	public SqlSessionTemplate getSqlSessionTemplate() {
		return sqlSessionTemplate;
	}

	public void setSqlSessionTemplate(SqlSessionTemplate sqlSessionTemplate) {
		this.sqlSessionTemplate = sqlSessionTemplate;
	}

	@Override
	public EDBType getDBType() {
		String dbId = sqlSessionTemplate.getConfiguration().getDatabaseId();
		if ("MySQL".equalsIgnoreCase(dbId)){
			return EDBType.MYSQL;
		}else if ("Oracle".equalsIgnoreCase(dbId)){
			return EDBType.ORACLE;
		}else if ("PostgreSQL".equalsIgnoreCase(dbId)){
			return EDBType.POSTGRESQL;
		}else{
			return EDBType.OTHER;
		}
	}

	@Override
	public int insert(String method, Object parameter) {
		return sqlSessionTemplate.insert(method, parameter);
	}

	@Override
	public int delete(String method, Object parameter) {
		return sqlSessionTemplate.delete(method, parameter);
	}

	@Override
	public <T> List<T> getList(String method, Object parameter) {
		return sqlSessionTemplate.selectList(method, parameter);
	}

	@Override
	public <T> List<T> getList(String method, Object parameter, Page page) {
		if (page == null){//分页参数为null,调用常规查询
			return getList(method, parameter);
		}
		return sqlSessionTemplate.selectList(method, parameter, new DalRowBounds(page));
	}

	@Override
	public int update(String method, Object parameter) {
		return sqlSessionTemplate.update(method, parameter);
	}

	@Override
	public <T> T  getObject(String method, Object parameter) {
		return sqlSessionTemplate.selectOne(method, parameter);
	}
	
}
